/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Sex-Adventure
 *
 *    AlarmConfig
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2012-1-9
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.markting.util;


import com.android.sdk.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * AlarmConfig of Sex-Adventure.
 *
 * @author zuochen
 */

public class AlarmConfig {
    private Activity mActivity;

    private static AlarmConfig mConfig;

    private SharedPreferences mSharedPreferences;

    private static final String IS_APP_FIRST_LUANCH = "is_app_first_luanch";

    private AlarmConfig(Activity activity) {
        this.mActivity = activity;
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity);
    }

    public static AlarmConfig getInstance(Activity activity) {
        if (null == mConfig) {
            mConfig = new AlarmConfig(activity);
        }
        return mConfig;
    }

    private boolean isAppFirstTimeLuanch() {
        return mSharedPreferences.getBoolean(IS_APP_FIRST_LUANCH, true);
    }

    private void changeAPPFirstTimeLuanchState() {
        mSharedPreferences.edit().putBoolean(IS_APP_FIRST_LUANCH, false).commit();
    }

    public boolean isShouldShowAlarm() {
        if (isAppFirstTimeLuanch()) {
            return true;
        }
        return false;
    }

    public void checkAlarm() {
        if (isShouldShowAlarm()) {
            showRateDialog();
        }// end if
    }

    private void showRateDialog() {
        new AlertDialog.Builder(mActivity)
                .setTitle(mActivity.getText(R.string.alarm_dialog_title))
                .setMessage(mActivity.getText(R.string.alarm_dialog_message))
                .setPositiveButton(mActivity.getText(R.string.alarm_dialog_btn_ok),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                changeAPPFirstTimeLuanchState();
                            }
                        })
                .setNegativeButton(mActivity.getText(R.string.alarm_dialog_btn_cancel),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mActivity.finish();
                                mConfig = null;
                            }
                        }).create().show();
    }
}
